const teamService = require('./teamService');
const cache = require('../module/tool/cache');
const timeTool = require('../module/tool/time');
const wxTool = require('../module/tool/wxapi');

/**
 * 专属配送订单通知
 * @param schoolId
 * @param orderData
 * @param businessType
 * @returns {Promise<void>}
 */
async function exclusiveOrderNotice (schoolId, orderData, businessType) {
    const res = await cache.readCache(schoolId);
    const timestamp = timeTool.getCurrentTimestamp();
    if (!res || (timestamp - parseInt(res)) > 300) {
        const hyperStaffList = await teamService.listStaff(schoolId, businessType === 'express' ? 'hyper_distribution_delivery' : 'heavy_delivery', 0, 10000);
        const openidList = hyperStaffList.data.map(v => v.openid);

        const commodityName = businessType === 'express' ? '快递代拿' : '重物搬运';
        const productName = businessType === 'express' ? '快递代拿' : '重物搬运';
        const price = orderData.price;
        const orderUser = businessType === 'express' ? orderData.delivery_info.contact : orderData.fetch_info.user_name;
        const createTime = orderData.create_time * 1000;
        const id = orderData._id.toString();

        const pages = businessType === 'express' ? '/pages/staff-center/hyper-express-center?schoolId=' + schoolId : '/pages/staff-center/heavy-handing-center?schoolId=' + schoolId;

        for (const openid of openidList) {
            wxTool.dormitoryOrderPlaceMessage(commodityName, openid, productName, price, orderUser, createTime, id, schoolId, pages).then();
        }
        await cache.writeCache(schoolId, timestamp, 86400);
    }
}

module.exports = {
    exclusiveOrderNotice
};
